local skel = fk.CreateSkill {
  name = "emo__duanyin",
}

Fk:loadTranslationTable{
  ["emo__duanyin"] = "断音",
  [":emo__duanyin"] = "每当你使用牌后，你可以获得四种花色各一张牌，然后〖断音〗失效直到你非因使用而失去四种花色的牌。",
  ["@emo__duanyin"] = "断音",
}

skel:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:getMark("@emo__duanyin") == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local suits, get = {}, {}
    local pile = table.simpleClone(room.draw_pile)
    table.insertTable(pile, room.discard_pile)
    while #pile > 0 and #suits < 4 do
      local id = table.remove(pile, math.random(#pile))
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit and table.insertIfNeed(suits, suit) then
        table.insert(get, id)
      end
    end
    if #get > 0 then
      room:setPlayerMark(player, "@emo__duanyin", {"log_spade","log_club","log_diamond","log_heart"})
      room:moveCardTo(get, Player.Hand, player, fk.ReasonPrey, self.name)
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@emo__duanyin") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("@emo__duanyin")
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason ~= fk.ReasonUse then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            table.removeOne(mark, Fk:getCardById(info.cardId):getSuitString(true))
          end
        end
      end
    end
    room:setPlayerMark(player, "@emo__duanyin", #mark > 0 and mark or 0)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__duanyin", 0)
end)

return skel
